package com.xinzhi.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.xinzhi.domain.Applicant;
import com.xinzhi.mapper.ApplicantMapper;
import com.xinzhi.service.ApplicantService;
import com.xinzhi.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ApplicantServiceimpl implements ApplicantService {


    @Autowired
    private ApplicantMapper applicantMapper;

    @Override
    public Result updateApplicant(Applicant applicant) {
        if (applicant != null) {

            int i = applicantMapper.updateById(applicant);
            //返回5受影响行数，进行判断，将判断后的信息返回给前端
            if (i > 0) {
                return Result.ok("修改成功");
            } else {
                return Result.error(500, "修改失败");
            }
        }
        return Result.error("请选择你要修改哪个团长的信息");
    }

    @Override
    public List<Applicant> selectList() {
        //查询全部团长的信息
        List<Applicant> applicants = applicantMapper.selectList(null);
        //将数据返还给前端
        return applicants;
    }

    @Override
    public Result insert(Applicant applicant) {
        int insert = applicantMapper.inapplicant(applicant);
        //判断一下返回的受影响行数，将状态返回给前端
        if (insert > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error(500, "添加失败");
        }
    }

    @Override
    public Result login(Applicant applicant) {
        List<Applicant> applicants = applicantMapper.selectList(null);
        if (applicant != null) {
            if (applicants.equals(applicant.getApplicantTel()) && applicants.equals(applicant.getPassword())) {
                return Result.ok("登录成功");
            }
        } else {
            return Result.error(500, "登录失败");
        }
        return Result.error("请重新输入");
    }

    @Override
    public Result updateRemove(Integer applicantId,Integer remove) {
        //通过前端给的id查询出对应团长的信息
        int i = applicantMapper.updateRemoveInt(applicantId, remove);
        if (i!=1){
            return Result.ok("成功");
        }else {
            return Result.ok("成功");
        }
    }


    @Override
    public Result updatepass(Integer applicantId, Integer pass) {
        // 参数校验 （id 是否为空  pass 是否为空 是否为 0/1）
        // 查询 id 对应的团长信息
        // 如果查询出的结果为空
        // 将请求参数的 pass 赋给查询出的 团长信息
        // 更新团长信息
        int updatepass = applicantMapper.updatepass(applicantId, pass);
        if (updatepass!= 0){
            return Result.ok("成功");
        }else {
            return Result.ok("成功");
        }
    }

    @Override
    public Result selectBypass(Integer pass) {
        List<Applicant> applicants = applicantMapper.selectBypass(pass);
        if (applicants.size() > 0){
            return Result.ok(applicants);
        }
        return Result.error(5005,"系统错误");


    }

    @Override
    public Result selectBypass1(Integer pass) {
        List<Applicant> applicants = applicantMapper.selectBypass(pass);
        if (applicants.size() > 0) {
            return Result.ok(applicants);
        }
        return Result.error(5005, "系统错误");


    }}


